//
//  FilmListViewController.m
//  豆瓣
//
//  Created by dllo on 15/11/23.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "FilmListViewController.h"
#import "FilmListTableViewCell.h"
#import "FilmDetailsViewController.h"
#import "FilmCollectionViewController.h"
#import "UIImageView+WebCache.h"
#import "netWorkHandle.h"
#import "Movie.h"

@interface FilmListViewController () <UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, retain) NSMutableArray *movieArr;
@property (nonatomic, retain) UITableView *tableV;

@end

@implementation FilmListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    
   /******************************** 电影列表界面 *************************************/
    
    
    self.navigationItem.title = @"电影";
    
    [self getData];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"btn_nav_collection@2x.png"] style:UIBarButtonItemStylePlain target:self action:@selector(rightAction)];
    
    self.navigationItem.hidesBackButton = YES;
    
    self.tableV = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 120)];
    self.tableV.delegate = self;
    self.tableV.dataSource = self;
    self.tableV.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:self.tableV];
    [self.tableV release];
    
}

// 获取并解析数据
- (void)getData {
    
    self.movieArr = [NSMutableArray array];
    
    [netWorkHandle getDataUnSyWithUrlStr:@"http://project.lanou3g.com/teacher/yihuiyun/lanouproject/movielist.php" block:^(NSData *netData) {
    
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:netData options:NSJSONReadingMutableContainers error:nil];
        NSArray *arr = [dic objectForKey:@"result"];
        
        for (NSDictionary *tempDic in arr) {
            
            Movie *movie = [[Movie alloc] init];
            [movie setValuesForKeysWithDictionary:tempDic];
            
            NSString *urlStr = [NSString stringWithFormat:@"http://project.lanou3g.com/teacher/yihuiyun/lanouproject/searchmovie.php?movieId=%@", [tempDic objectForKey:@"movieId"]];
            
            [netWorkHandle getDataUnSyWithUrlStr:urlStr block:^(NSData *netData) {
               
                NSDictionary *adic = [[NSJSONSerialization JSONObjectWithData:netData options:NSJSONReadingMutableContainers error:nil] objectForKey:@"result"];
                [movie setValuesForKeysWithDictionary:adic];
  
            }];
            movie.image = [tempDic objectForKey:@"pic_url"];
           
            [self.movieArr addObject:movie];
            [movie release];
        }
        
        [self.tableV reloadData];
    }];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return [self.movieArr count];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return 155;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *cellStr = @"cell";
    
    FilmListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellStr];
    if (nil == cell) {
        
        cell = [[FilmListTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellStr];
    }
    
    Movie *movie = [self.movieArr objectAtIndex:indexPath.row];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    [cell.imageV sd_setImageWithURLStr:movie.image];
    cell.nameLabel.text = movie.movieName;
    
    return cell;
}

- (void)viewWillAppear:(BOOL)animated {
    
    [super viewWillAppear:animated];
    while (self.movieArr.count == 0) {
        [self.tableV reloadData];
    }
}

// 跳转到详细界面
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    FilmDetailsViewController *filmDetailsVC = [[FilmDetailsViewController alloc] init];
    
    Movie *movie = [self.movieArr objectAtIndex:indexPath.row];
    filmDetailsVC.movie = movie;
    
    [self.navigationController pushViewController:filmDetailsVC animated:YES];
    [filmDetailsVC release];
}

// 切换缩略图
- (void)rightAction {
    
    FilmCollectionViewController *filmVC = [[FilmCollectionViewController alloc] init];
    [self.navigationController pushViewController:filmVC animated:YES];
    filmVC.movieArr = self.movieArr;
    [filmVC release];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}

@end
